window.$ = window.jQuery = require("jquery");
require("./rem");
require('../../css/reset2016.css', 'css|style');
require('../../css/public2016.css', 'css|style');
require('../../css/yiyuan.css', 'css|style');

var API = require("../libs/ApiUtil");
var md5 = require("md5");
var GET = API.GetURLParams();
var openId = GET["openId"];
var shopNo = GET["mch_id"];
var shopName;

var initAmount = "0";
var header = {
    "Content-Type": "application/json; charset=UTF-8",
    'X-Client-Agent': "weixin",
    'X-APIVersion': "2.0",
    'X-Client-ID': '123456'
};

function getMerchantName() {
    var params = new Object();
    params.shopNo = shopNo;
    var data = JSON.stringify(params);
    API.postWeChatPay("shopInfo/getShopInfoByShopNo", data, header, function (res) {
        if (res.errorCode == 0) {
            shopName = res.data.shortName;
            $("#shopName").html(shopName);
        }
    })
}
getMerchantName();

function inputAmount(amount) {
    if(amount == '.' && initAmount.indexOf(".") < 0) {
        initAmount = initAmount + amount;
    } else {
        if(initAmount.indexOf(".") > 0) {
            var decimal = initAmount.substr(initAmount.indexOf("."));
            if(decimal.length >= 3) {
                return;
            }
        }

        var tempAmount;
        if(initAmount == '0') {
            tempAmount = amount;
        } else {
            tempAmount = initAmount + amount;
        }
        if(Number(tempAmount) <= 10000) {
            initAmount = tempAmount;
        }
    }

    $("#payAmount").val("¥" + initAmount);
    if(Number(initAmount) > 0) {
        $("#pay-disable-key").css("display", "none");
        $("#pay-enable-key").css("display", "");
    }
}

$("#key1").click(function () {
    inputAmount("1");
})
$("#key2").click(function () {
    inputAmount("2");
})
$("#key3").click(function () {
    inputAmount("3");
})
$("#key4").click(function () {
    inputAmount("4");
})
$("#key5").click(function () {
    inputAmount("5");
})
$("#key6").click(function () {
    inputAmount("6");
})
$("#key7").click(function () {
    inputAmount("7");
})
$("#key8").click(function () {
    inputAmount("8");
})
$("#key9").click(function () {
    inputAmount("9");
})
$("#key0").click(function () {
    inputAmount("0");
})
$("#key_dot").click(function () {
    inputAmount('.');
})

$("#key_del").click(function () {
    var tempAmount;
    if(initAmount.length > 1) {
        initAmount = initAmount.substr(0, initAmount.length - 1);
        tempAmount = initAmount;
    } else {
        initAmount = "0";
        tempAmount = "";
        $("#pay-disable-key").css("display", "");
        $("#pay-enable-key").css("display", "none");
    }
    $("#payAmount").val("¥" + tempAmount);
})

$("#pay-enable-key").click(function () {
    showMask();
    var params = new Object();
    params.merchantId = shopNo;
    params.merchantName = shopName;
    params.openId = openId;
    params.orderAmt = initAmount;
    params.tradeType = "JSAPI";
    var data = JSON.stringify(params);
    API.postWeChatPay("pay/userScan", data, header, function (res) {
        if (res.errorCode == 0) {
            if(typeof WeixinJSBridge == "undefined") {
                if(document.addEventListener ){
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent){
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
                hideMask();
            } else {
                onBridgeReady(res.data);
            }
        } else {
            alert("网络异常，请稍后再试");
            hideMask();
        }
    })
})

function onBridgeReady(result){
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
            "appId": result.appId,     //公众号名称，由商户传入
            "timeStamp": result.timeStamp,         //时间戳，自1970年以来的秒数
            "nonceStr": result.nonceStr, //随机串
            "package": result.package,
            "signType": result.signType,         //微信签名方式：
            "paySign": result.paySign //微信签名
        },
        function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok") {
                window.location.href = "http://weixin.hzyisu.com/#/pay/paySuccess?shopNo=" + shopNo + "&shopName=" + shopName + "&amount=" + initAmount;
            } else {
                alert("网络异常，请稍后再试");
                hideMask();
            }
        }
    );
}

function showMask(){
    $("#mask").css("height",$(document).height());
    $("#mask").css("width",$(document).width());
    $("#mask").show();
}
//隐藏遮罩层
function hideMask(){
    $("#mask").hide();
}
